package vn.vtsmart.vtcalendarsync;

import java.util.Calendar;

public class CalendarEvent {

	private String mLocation;
	private String mMainPerson;
	private String mAttendtion;
	private String mPreparePerson;
	private String mContent;
	private String mTime;
	private String mDate;

	private long mStartMillis;
	private long mEndMillis;
	private int mYear;
	private int mDay;
	private int mMonth;
	private int mHourStart;
	private int mMinStart;
	private int mHourEnd;
	private int mMinEnd;

	public String getmLocation() {
		return mLocation;
	}

	@Override
	public String toString() {
		return "[mTime=" + mTime + ", mDate=" + mDate + ", mLocation="
				+ mLocation + ", mMainPerson=" + mMainPerson + ", mAttendtion="
				+ mAttendtion + ", mPreparePerson=" + mPreparePerson
				+ ", mContent=" + mContent + "]\n";
	}

	public CalendarEvent(String mLocation, String mMainPerson,
			String mAttendtion, String mPreparePerson, String mContent,
			String mTime, String mDate) {
		super();
		this.mLocation = mLocation;
		this.mMainPerson = mMainPerson;
		this.mAttendtion = mAttendtion;
		this.mPreparePerson = mPreparePerson;
		this.mContent = mContent;
		this.mTime = mTime.toLowerCase();
		this.mDate = mDate.replaceAll("[\t\n\r]", " ").toLowerCase();
		convertTime();
	}

	public void setmLocation(String mLocation) {
		this.mLocation = mLocation;
	}

	public String getmMainPerson() {
		return mMainPerson;
	}

	public void setmMainPerson(String mMainPerson) {
		this.mMainPerson = mMainPerson;
	}

	public String getmAttendtion() {
		return mAttendtion;
	}

	public void setmAttendtion(String mAttendtion) {
		this.mAttendtion = mAttendtion;
	}

	public String getmPreparePerson() {
		return mPreparePerson;
	}

	public void setmPreparePerson(String mPreparePerson) {
		this.mPreparePerson = mPreparePerson;
	}

	public String getmContent() {
		return mContent;
	}

	public void setmContent(String mContent) {
		this.mContent = mContent;
	}

	public String getmTime() {
		return mTime;
	}

	public void setmTime(String mTime) {
		this.mTime = mTime;
	}

	public String getmDate() {
		return mDate;
	}

	public void setmDate(String mDate) {
		this.mDate = mDate;
	}

	public void appendData(String append_mainPerson,
			String append_preparePerson, String append_attendtion) {
		mMainPerson = mMainPerson + ", " + append_mainPerson;
		mPreparePerson = mPreparePerson + ", " + append_preparePerson;
		mAttendtion = mAttendtion + ", " + append_attendtion;
	}

	public String getDescription() {
		StringBuilder description = new StringBuilder();
		description.append("Nội dung:\n");
		description.append(mContent + "\n\n");
		description.append("Địa điểm:\n");
		description.append(mLocation + "\n\n");
		description.append("Chủ trì:\n");
		description.append(mMainPerson + "\n\n");
		description.append("Chuẩn bị:\n");
		description.append(mPreparePerson + "\n\n");
		description.append("Thành phần tham gia:\n");
		description.append(mAttendtion);

		return description.toString();
	}

	private void getDayAndMonth() {
		String[] parts = mDate.split(" ");
		String day_month = parts[2].trim();
		String[] date = null;
		if(parts[2].indexOf("-") != -1) {
			date = day_month.split("-");
		} else if(parts[2].indexOf("/") != -1) {
			date = day_month.split("/");
		}
		
		String day = date[0].trim();
		String month = date[1].trim();
		Calendar now = Calendar.getInstance();
		mYear = now.get(Calendar.YEAR);
		mDay = Integer.parseInt(day);
		mMonth = Integer.parseInt(month);
	}

	private void convertTime() {
		getDayAndMonth();
		String[] parts = mTime.split("-");

		String strStartTime = parts[0].trim();
		String[] start = strStartTime.split("h");
		String hourStart = start[0].trim();
		String minuteStart;
		if (start.length == 1) {
			minuteStart = "0";
		} else {
			minuteStart = start[1].trim();
		}

		mHourStart = Integer.parseInt(hourStart);
		mMinStart = Integer.parseInt(minuteStart);
		Calendar startTime = Calendar.getInstance();
		startTime.set(mYear, mMonth - 1, mDay, mHourStart, mMinStart);
		mStartMillis = startTime.getTimeInMillis();

		String strEndTime = parts[1].trim();
		String[] end = strEndTime.split("h");
		String hourEnd = end[0].trim();
		String minEnd = null;
		if (start.length == 1) {
			minEnd = "0";
		} else {
			minEnd = end[1].trim();
		}

		mHourEnd = Integer.parseInt(hourEnd);
		mMinEnd = Integer.parseInt(minEnd);
		Calendar endTime = Calendar.getInstance();
		endTime.set(mYear, mMonth - 1, mDay, mHourEnd, mMinEnd);
		mEndMillis = endTime.getTimeInMillis();

	}

	public long getmStartMillis() {
		return mStartMillis;
	}

	public long getmEndMillis() {
		return mEndMillis;
	}

}
